import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    cartList: [],
  },
  mutations: {
    addCount(state,oldproduct) {
      oldproduct.count += 1
    },
    addToCart(state,payload) {
      state.cartList.push(payload)
    }
  },
  actions: {
    addCart(context,payload) {      
      let oldproduct = context.state.cartList.find((item) => {
        return item.iid === payload.iid
      })
      if(oldproduct){
        context.commit('addCount',oldproduct)
      } else {
        payload.count = 1;
        payload.checked = false;
        context.commit('addToCart',payload)
      }
    }
  },
  modules: {
  }
})
